package nccloud.web.uapbd.ref.org;

import java.util.Map;
import nc.uap.utils.SQLHelper;
import nccloud.commons.lang.ArrayUtils;
import nccloud.commons.lang.StringUtils;
import nccloud.framework.web.processor.refgrid.RefQueryInfo;
import nccloud.framework.web.ui.meta.RefMeta;
import nccloud.framework.web.ui.meta.TreeRefMeta;
import nccloud.pubitf.platform.db.SqlParameterCollection;
import nccloud.web.refer.DefaultTreeRefAction;

public class BusinessUnitTreeRef extends DefaultTreeRefAction {
	public BusinessUnitTreeRef() {
		setShowDisabledData(Boolean.valueOf(false));
		setUnitPkKey("pk_group");
		setResourceCode("org");
	}

	public String getExtraSql(RefQueryInfo paramRefQueryInfo, RefMeta paramRefMeta) {
		Map param = paramRefQueryInfo.getQueryCondition();
		StringBuffer sql = new StringBuffer("");

		String isMutiGroup = (String) param.get("isMutiGroup");
		if ((isMutiGroup != null) && ("Y".equals(isMutiGroup))) {
			return sql.toString();
		}
		
		if(StringUtils.isNotEmpty((String) param.get("center"))) {
			sql = sql.append(" and def2 = 'true' ");
		}
		
		sql = sql.append(" and isbusinessunit='Y' ");
		sql = sql.append(" and pk_group=? ");
		String pk_org = (String) param.get("pk_org");
		if (StringUtils.isNotBlank(pk_org)) {
			sql.append(" and pk_org=?");
		}

		String pk_orgs = (String) param.get("pk_orgs");
		if (StringUtils.isNotBlank(pk_orgs)) {
			String[] pk_orgarr = pk_orgs.split(",");

			if (!ArrayUtils.isEmpty(paramRefQueryInfo.getFilterPks())) {
				pk_orgarr = (String[]) ArrayUtils.addAll(pk_orgarr, paramRefQueryInfo.getFilterPks());
			}

			paramRefQueryInfo.setFilterPks(pk_orgarr);
		}
		String orgtype5 = (String) paramRefQueryInfo.getQueryCondition().get("orgtype5");
		String orgtype15 = (String) paramRefQueryInfo.getQueryCondition().get("orgtype15");
		if ((orgtype5 != null) && (orgtype15 != null)) {
			sql.append(" and (orgtype5='" + orgtype5 + "' or orgtype15 ='" + orgtype15 + "') ");
		}

		return sql.toString();
	}

	public SqlParameterCollection getExtraSqlParameter(RefQueryInfo paramRefQueryInfo, RefMeta paramRefMeta) {
		Map param = paramRefQueryInfo.getQueryCondition();

		String isMutiGroup = (String) param.get("isMutiGroup");
		if ((isMutiGroup != null) && ("Y".equals(isMutiGroup))) {
			return null;
		}

		String pk_org = (String) param.get("pk_org");
		SqlParameterCollection sc = new SqlParameterCollection();
		String pk_group = getPk_group(paramRefQueryInfo);
		sc.addVarChar(pk_group);
		if (StringUtils.isNotBlank(pk_org)) {
			sc.addVarChar(pk_org);
		}

		return sc;
	}

	public TreeRefMeta getRefMeta(RefQueryInfo refQueryInfo) {
		TreeRefMeta trf = new TreeRefMeta();
		trf.setCodeField("code");
		trf.setNameField("name");
		trf.setPidField("pk_fatherorg");
		trf.setPkField("pk_org");
		trf.setMutilLangNameRef(true);
		String isMutiGroup = (String) refQueryInfo.getQueryCondition().get("isMutiGroup");

		if ((StringUtils.isNotEmpty(isMutiGroup)) && ("Y".equals(isMutiGroup))) {
			trf.setExtraFields(new String[] { "code", "name", "ReturnOnTree" });
			trf.setTableName(getTableName(refQueryInfo));
			return trf;
		}
		trf.setExtraFields(new String[] { "name", "gname", "isretail", "pk_controlarea", "orgtype33", "orgtype15",
				"pk_corp", "orgtype7", "orgtype5", "orgtype15" });

		StringBuffer sb = new StringBuffer();

		sb.append(" (select org_orgs.code code," + SQLHelper.getMultiLangNameColumn("org_orgs.name") + " name,"
				+ SQLHelper.getMultiLangNameColumn("org_group.name")
				+ " gname,org_orgs.pk_fatherorg pk_fatherorg,org_orgs.pk_org pk_org,org_orgs.enablestate enablestate,org_orgs.pk_controlarea pk_controlarea,");

		sb.append(
				" org_orgs.name2 name2,org_orgs.name3 name3,org_orgs.name4 name4,org_orgs.name5 name5,org_orgs.name6 name6,");

		sb.append(" org_orgs.isbusinessunit isbusinessunit,org_orgs.pk_group pk_group,org_orgs.dr dr,");
		sb.append(
				" org_orgs.orgtype7 orgtype7,org_orgs.orgtype8 orgtype8,org_orgs.orgtype5 orgtype5,org_orgs.orgtype9,org_orgs.orgtype2 orgtype2,org_orgs.orgtype33,org_orgs.orgtype34,org_orgs.orgtype15,org_orgs.isretail,org_orgs.pk_corp as pk_corp,org_orgs.def2 def2");

		sb.append(" from org_orgs org_orgs left join org_group org_group on org_orgs.pk_group=org_group.pk_group) a ");

		trf.setTableName(sb.toString());

		sb.setLength(0);

		return trf;
	}

	public String getTableName(RefQueryInfo refQueryInfo) {
		String orgSql = "select case when pk_fatherorg='~' or pk_fatherorg is null then pk_group else pk_fatherorg end as pk_fatherorg,code,name,name2,name3,name4,name5,name6,pk_org,enablestate,'Y' ReturnOnTree from org_orgs where isbusinessunit = 'Y' and enablestate = 2";
		String groupSql = "select pk_fathergroup as pk_fatherorg,code, name,name2,name3,name4,name5,name6,pk_group as pk_org,enablestate,'N' ReturnOnTree from org_group where enablestate = 2";
		String tableName = "(" + orgSql + " union " + groupSql + ")org_plus_group";
		return tableName;
	}
}